package com.geega.geetest;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.FileWriter;
import java.io.IOException;

@Slf4j
public class GenerateMain {

    private static final String JAVA_SRC = "src/main/java/";

    public static void main(String[] args) throws IOException {
        String packageName = "com.example.demo";// 包名
        String modelName = "Article";// 实体类
        String tablePrefix = "t_";// 表前缀
        createFiles(packageName, modelName, tablePrefix);
        log.info("逆向工程创建成功！");
    }

    private static void createFiles(String packageName, String modelName, String tablePrefix) throws IOException {
        // 创建entity
        String model = ResourceUtil.readUtf8Str("Model.txt");
        String modelReplace = StrUtil.replace(model, "${PACKAGE_NAME}", packageName).replace("${MODEL_NAME}", modelName).replace("${MODEL_NAME_LOWER}", modelName.toLowerCase()).replace("${TABLE_PREFIX}", tablePrefix);
        FileWriter modelWriter = new FileWriter(JAVA_SRC + packageName.replaceAll("\\.", "/") + "/entity/" + modelName + ".java");
        modelWriter.write(modelReplace);
        modelWriter.close();
        // 创建dao
        String dao = ResourceUtil.readUtf8Str("Dao.txt");
        String daoReplace = StrUtil.replace(dao, "${PACKAGE_NAME}", packageName).replace("${MODEL_NAME}", modelName);
        FileWriter daoWriter = new FileWriter(JAVA_SRC + packageName.replaceAll("\\.", "/") + "/dao/" + modelName + "Dao.java");
        daoWriter.write(daoReplace);
        daoWriter.close();
        // 创建service
        String service = ResourceUtil.readUtf8Str("Service.txt");
        String serviceReplace = StrUtil.replace(service, "${PACKAGE_NAME}", packageName).replace("${MODEL_NAME}", modelName).replace("${MODEL_NAME_LOWER}", modelName.toLowerCase());
        FileWriter serviceWriter = new FileWriter(JAVA_SRC + packageName.replaceAll("\\.", "/") + "/service/" + modelName + "Service.java");
        serviceWriter.write(serviceReplace);
        serviceWriter.close();
        // 创建controller
        String controller = ResourceUtil.readUtf8Str("Controller.txt");
        String controllerReplace = StrUtil.replace(controller, "${PACKAGE_NAME}", packageName).replace("${MODEL_NAME}", modelName).replace("${MODEL_NAME_LOWER}", modelName.toLowerCase());
        FileWriter controllerWriter = new FileWriter(JAVA_SRC + packageName.replaceAll("\\.", "/") + "/controller/" + modelName + "Controller.java");
        controllerWriter.write(controllerReplace);
        controllerWriter.close();
        // 创建mapper
        String mapper = ResourceUtil.readUtf8Str("Mapper.txt");
        String mapperReplace = StrUtil.replace(mapper, "${PACKAGE_NAME}", packageName).replace("${MODEL_NAME}", modelName);
        FileWriter mapperWriter = new FileWriter(JAVA_SRC + packageName.replaceAll("\\.", "/") + "/mapper/" + modelName + "Mapper.java");
        mapperWriter.write(mapperReplace);
        mapperWriter.close();
        // 创建idDto
        String idDto = ResourceUtil.readUtf8Str("IdDto.txt");
        String idDtoReplace = StrUtil.replace(idDto, "${PACKAGE_NAME}", packageName).replace("${MODEL_NAME}", modelName);
        FileWriter idDtoWriter = new FileWriter(JAVA_SRC + packageName.replaceAll("\\.", "/") + "/dto/" + modelName + "IdDto.java");
        idDtoWriter.write(idDtoReplace);
        idDtoWriter.close();
        // 创建saveDto
        String saveDto = ResourceUtil.readUtf8Str("SaveDto.txt");
        String saveDtoReplace = StrUtil.replace(saveDto, "${PACKAGE_NAME}", packageName).replace("${MODEL_NAME}", modelName);
        FileWriter saveDtoWriter = new FileWriter(JAVA_SRC + packageName.replaceAll("\\.", "/") + "/dto/" + modelName + "SaveDto.java");
        saveDtoWriter.write(saveDtoReplace);
        saveDtoWriter.close();
    }

}
